package com.prj.ufdm.auth.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.prj.ufdm.auth.bpo._impl.SysUserBPOImpl;
import com.prj.ufdm.auth.enums.AuthRetCodeEnum;
import com.prj.ufdm.auth.model.SysUser;
import com.prj.ufdm.core.bean.PageBean;
import com.prj.ufdm.core.dto.RetData;
import com.prj.ufdm.core.exception.BaseException;
import com.prj.ufdm.core.util.UfdmDateUtil;
import com.prj.ufdm.core.util.UfdmMd5Util;
import com.prj.ufdm.core.util.UfdmStringUtil;
import com.prj.ufdm.core.util.UfdmUUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 用户管理控制类
 * @author 胡义振  
 * @date 2018年3月26日
 */
@RequestMapping(value = "/api/auth/sysuser")
@Controller
public class SysUserController {
	
	@Autowired
	SysUserBPOImpl sysUserBPO;

	/**
	 * 查询所有系统用户
	 * @param 
	 *     sysUser 系统用户对象 （为空 查询所有）
	 *     pageBean 分页查询对象
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doGetList")
	@ResponseBody
	public RetData<PageInfo<SysUser>> doGetList(SysUser sysUser,PageBean pageBean) throws Exception {
		RetData<PageInfo<SysUser>> retData = new RetData<PageInfo<SysUser>>();
		PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		List<SysUser> listSysUser = sysUserBPO.doGetList(sysUser);
		PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(listSysUser);
		retData.setRetBody(pageInfo);
		return retData;
	}
	/**
	 * 查询当前用户
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doGetUser")
	@ResponseBody
	public RetData<SysUser> doGetUser(SysUser sysUser) throws Exception {
		RetData<SysUser> retData = new RetData<SysUser>();
		SysUser user = sysUserBPO.doGetById(sysUser.getId());
		retData.setRetBody(user);
		return retData;
	}
	
	/**
	 * 查询角色用户
	 * @param 
	 *     sysUser 系统用户对象 （为空 查询所有）
	 *     pageBean 分页查询对象
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doGetListWithRoleId")
	@ResponseBody
	public RetData<PageInfo<SysUser>> doGetListWithRoleId(SysUser sysUser,PageBean pageBean) throws Exception {
		
		if(sysUser==null || "".equals(UfdmStringUtil.replaceAllBlank(sysUser.getRoleId()))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0103001.getCode(),AuthRetCodeEnum.RET_CODE_0103001.getMessage());
		}
		RetData<PageInfo<SysUser>> retData = new RetData<PageInfo<SysUser>>();
		PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		List<SysUser> listSysUser = sysUserBPO.doGetSysUserListWithRoleId(sysUser);
		PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(listSysUser);
		retData.setRetBody(pageInfo);
		return retData;
	}
	
	/**
	 * 保存系统用户
	 * @param sysUser 
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doSave")
	@ResponseBody
	public RetData<String> doSave(SysUser sysUser) throws Exception {
		RetData<String> retData = new RetData<String>();
		// 用户名不能为空
		if(sysUser==null || "".equals(UfdmStringUtil.replaceAllBlank(sysUser.getUsername()))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102001.getCode(),AuthRetCodeEnum.RET_CODE_0102001.getMessage());
		}
		else {
			// 密码不能为空
			if("".equals(UfdmStringUtil.replaceAllBlank(sysUser.getPassword()))) {
				throw new BaseException(AuthRetCodeEnum.RET_CODE_0102002.getCode(),AuthRetCodeEnum.RET_CODE_0102002.getMessage());
			}
			// 用户名已存在
			if(sysUserBPO.doCheckSysUserLoginNameExist(sysUser.getUsername(), "")) {
				throw new BaseException(AuthRetCodeEnum.RET_CODE_0102003.getCode(),AuthRetCodeEnum.RET_CODE_0102003.getMessage());
			}
		}
		sysUser.setId(UfdmUUIDUtil.getUUID());
		sysUser.setPassword(UfdmMd5Util.MD5Encode(sysUser.getPassword()));
		sysUser.setCreateTime(UfdmDateUtil.getCurrentTime());
		sysUserBPO.doSave(sysUser);
		return retData;
	}
	
	/**
	 * 修改用户信息
	 * @param sysUser 
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doMod")
	@ResponseBody
	public RetData<String> doMod(SysUser sysUser) throws Exception {
		RetData<String> retData = new RetData<String>();
		// 用户ID不能为空
		if(sysUser==null || sysUser.getId()==null || "".equals(UfdmStringUtil.replaceAllBlank(sysUser.getId()))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102004.getCode(),AuthRetCodeEnum.RET_CODE_0102004.getMessage());
		}
		
		// 检查用户ID是否存在
		if(sysUserBPO.doGetById(sysUser.getId())==null) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102005.getCode(),AuthRetCodeEnum.RET_CODE_0102005.getMessage());
		}
		
		// 用户名不能为
		if("".equals(UfdmStringUtil.replaceAllBlank(sysUser.getUsername()))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102001.getCode(),AuthRetCodeEnum.RET_CODE_0102001.getMessage());
		}
		
		// 用户名已存在
		else if(sysUserBPO.doCheckSysUserLoginNameExist(sysUser.getUsername(), sysUser.getId())) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102003.getCode(),AuthRetCodeEnum.RET_CODE_0102003.getMessage());
		}
		
		// 密码为空，不修改密码
		if(!"".equals(UfdmStringUtil.replaceAllBlank(sysUser.getPassword()))) {
		    sysUser.setPassword(UfdmMd5Util.MD5Encode(sysUser.getPassword()));
		}
		sysUserBPO.doModById(sysUser);
		return retData;
	}
	
	/**
	 * 修改用户密码
	 * @param sysUser 
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doModPass")
	@ResponseBody
	public RetData<String> doModPass(SysUser sysUser) throws Exception {
		RetData<String> retData = new RetData<String>();
		// 用户名不能为空
		if(sysUser==null || sysUser.getUsername()==null || "".equals(UfdmStringUtil.replaceAllBlank(sysUser.getUsername()))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102001.getCode(),AuthRetCodeEnum.RET_CODE_0102001.getMessage());
		}
		// 原密码不能为空
		if(sysUser==null || sysUser.getOldPassword()==null || "".equals(UfdmStringUtil.replaceAllBlank(sysUser.getOldPassword()))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102006.getCode(),AuthRetCodeEnum.RET_CODE_0102006.getMessage());
		}
		// 新密码不能为空
		if(sysUser==null || sysUser.getPassword()==null || "".equals(UfdmStringUtil.replaceAllBlank(sysUser.getPassword()))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102002.getCode(),AuthRetCodeEnum.RET_CODE_0102002.getMessage());
		}
		// 检查老密码是否正确
		SysUser validSysUser = sysUserBPO.doGetSysUserByUserNameAndPassword(sysUser.getUsername(), UfdmMd5Util.MD5Encode(sysUser.getOldPassword()));
		if(validSysUser==null) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102007.getCode(),AuthRetCodeEnum.RET_CODE_0102007.getMessage());
		}
		validSysUser.setPassword(UfdmMd5Util.MD5Encode(sysUser.getPassword()));
		sysUserBPO.doModById(validSysUser);
		retData.setRetBody("密码修改成功");
		return retData;
	}
	
	/**
	 * 删除用户
	 * @param id 要删除的用户ID（多个ID，逗号分隔）
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doDel")
	@ResponseBody
	public RetData<String> doDelRole(String id) throws Exception {
		RetData<String> retData = new RetData<String>();
		sysUserBPO.doRmSysUserById(id);
		return retData;
	}
	
	/**
	 * 添加角色的用户
	 * @param 
	 *      userId      用户ID
	 *      roleIds     角色ID（多个用","号分隔）
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doAddUserRole")
	@ResponseBody
	public RetData<String> doAddRoleUser(String userId,String roleIds) throws Exception {
		RetData<String> retData = new RetData<String>();
		// 角色ID不能为空
		if("".equals(UfdmStringUtil.replaceAllBlank(roleIds))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0103001.getCode(),AuthRetCodeEnum.RET_CODE_0103001.getMessage());
		}
		// 用户ID不能为空
		if("".equals(UfdmStringUtil.replaceAllBlank(userId))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102004.getCode(),AuthRetCodeEnum.RET_CODE_0102004.getMessage());
		}
		sysUserBPO.doAddSysUserRole(userId,roleIds);
		return retData;
	}
	
	/**
	 * 删除用户角色
	 * @param 
	 *      userId      用户ID
	 *      roleIds     角色ID（多个用","号分隔）
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doDelUserRole")
	@ResponseBody
	public RetData<String> doDelUserRole(String userId,String roleIds) throws Exception {
		RetData<String> retData = new RetData<String>();
		// 角色ID不能为空
		if("".equals(UfdmStringUtil.replaceAllBlank(roleIds))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0103001.getCode(),AuthRetCodeEnum.RET_CODE_0103001.getMessage());
		}
		// 用户ID不能为空
		if("".equals(UfdmStringUtil.replaceAllBlank(userId))) {
			throw new BaseException(AuthRetCodeEnum.RET_CODE_0102004.getCode(),AuthRetCodeEnum.RET_CODE_0102004.getMessage());
		}
		sysUserBPO.doRmSysUserRole(userId,roleIds);
		return retData;
	}
	
}
